package com.woniuxy.kaer.user.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.commons.util.OSSUtil;
import com.woniuxy.kaer.user.model.CarInfo;
import com.woniuxy.kaer.user.mapper.CarInfoMapper;
import com.woniuxy.kaer.user.param.AddCarParam;
import com.woniuxy.kaer.user.service.CarInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@Service
public class CarInfoServiceImpl extends ServiceImpl<CarInfoMapper, CarInfo> implements CarInfoService {

    @Resource
    private CarInfoMapper carInfoMapper;
    @Resource
    private OSSUtil ossUtil;

    @Override
    public void addCar(Integer userId, AddCarParam param) throws Exception {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("license_plate",param.getLicensePlate());
        CarInfo carInfo = carInfoMapper.selectOne(queryWrapper);
        if(carInfo!= null){
            throw new Exception("该车辆已存在");
        }
//        //文件上传阿里云oss
//        String originalFilename = param.getCarPicture().getOriginalFilename();
//        //获取文件后缀
//        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//        //生成新的文件名
//        Snowflake snowflake = new Snowflake(1, 1);
//        String newFilename = snowflake.nextId() + suffix;
//        //上传文件到oss
//        ossUtil.uploadFile(param.getCarPicture().getInputStream(), newFilename,"guojiandong");
        CarInfo car = new CarInfo();
        car.setUserId(userId);
        car.setCarBrand(param.getCarBrand());
        car.setCarColor(param.getCarColor());
        car.setCarModel(param.getCarModel());
        car.setLicensePlate(param.getLicensePlate());
        car.setCarType(param.getCarType());
        car.setCarState(Data.CAR_STATE_NORMAL);
        car.setAccident(Data.CAR_NOT_INSURED);
        car.setMileage(param.getMileage());
        car.setMaintenanceTime(param.getMaintenanceTime());
//        car.setCarPicture(newFilename);
        carInfoMapper.insert(car);

    }
}
